/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvas;

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import midlet.MidletPlay;

/**
 * @author LTTDNQ
 */
public class MIDPCanvasOptions extends GameCanvas implements CommandListener {

    private Graphics _g;
    private Command _cmExit;
    private int _chieudai = getWidth();
    private int _chieucao = getHeight();
    private MidletPlay _midlet;
    private Image imgBg, imgSelect;
    private int _viTri;
    private int _intLevel = 1, _intAnimals = 1;
    private String _strSound = "ON";

    /**
     * constructor
     */
    public MIDPCanvasOptions(MidletPlay midlet) {

        super(false);
        this._midlet = midlet;
        _g = this.getGraphics();
        try {
            _cmExit = new Command("Exit", Command.EXIT, 1);
            // Set up this canvas to listen to command events
            setCommandListener(this);
            // Add the Exit command

            addCommand(_cmExit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * paint
     */
    public void paintOptions() {
        try {
            imgBg = Image.createImage("/Images/imgBackground.png");
            imgSelect = Image.createImage("/Images/ga.png");
            _g.drawImage(imgBg, 0, 0, Graphics.LEFT | Graphics.TOP);
            _g.drawImage(imgSelect, _chieudai * 2 / 10, _chieucao * 4 / 10, Graphics.LEFT | Graphics.TOP);
            Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
            _g.setColor(255, 0, 0);
            _g.setFont(font);
            _g.drawString("OPTIONS", _chieudai * 4 / 10, _chieucao * 3 / 10 + 5, Graphics.LEFT | Graphics.TOP);
            _g.drawString("Animals:", _chieudai * 3 / 10, _chieucao * 4 / 10 + 5, Graphics.LEFT | Graphics.TOP);
            _g.drawString(String.valueOf(_intAnimals), _chieudai * 6 / 10, _chieucao * 4 / 10 + 5, Graphics.LEFT | Graphics.TOP);
            _g.drawString("Levels:", _chieudai * 3 / 10, _chieucao * 5 / 10 + 5, Graphics.LEFT | Graphics.TOP);
            _g.drawString(String.valueOf(_intLevel), _chieudai * 6 / 10, _chieucao * 5 / 10 + 5, Graphics.LEFT | Graphics.TOP);
            _g.drawString("Sound:", _chieudai * 3 / 10, _chieucao * 6 / 10 + 5, Graphics.LEFT | Graphics.TOP);
            _g.drawString(_strSound, _chieudai * 6 / 10, _chieucao * 6 / 10 + 5, Graphics.LEFT | Graphics.TOP);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Draw_nextItem() {
        if (_viTri == 1) {
            _g.drawImage(imgSelect, _chieudai * 2 / 10, _chieucao * 4 / 10, Graphics.LEFT | Graphics.TOP);
        } else if (_viTri == 2) {
            _g.drawImage(imgSelect, _chieudai * 2 / 10, _chieucao * 5 / 10, Graphics.LEFT | Graphics.TOP);
        } else {
            _g.drawImage(imgSelect, _chieudai * 2 / 10, _chieucao * 6 / 10, Graphics.LEFT | Graphics.TOP);
        }
        flushGraphics();

    }

    private void changeAnimals() {
    }

    private void changeLevel() {
    }

    private void changeSound() {
        if(_strSound.compareTo("ON") == 0){
            _strSound = "OFF";
            _g.drawString(_strSound, _chieudai * 6 / 10, _chieucao * 6 / 10 + 5, Graphics.LEFT | Graphics.TOP);
        }
        else{
              _strSound = "ON";
            _g.drawString(_strSound, _chieudai * 6 / 10, _chieucao * 6 / 10 + 5, Graphics.LEFT | Graphics.TOP);
        }
        flushGraphics();
    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {
        int action = getGameAction(keyCode);
        switch (action) {
            case 8:
                if (_viTri == 1) {
                    _midlet.vaoManHinhChoi();
                } else if (_viTri == 2) {
                    _midlet.vaoManHinhOptions();
                } else if (_viTri == 3) {
                    _midlet.vaoManHinhHowToPlay();
                }
            case DOWN:
                _viTri = _viTri + 1;
                if (_viTri > 3) {
                    _viTri = 1;
                }
                Draw_nextItem();
                break;
            case UP:
                _viTri = _viTri - 1;
                if (_viTri < 1) {
                    _viTri = 5;
                }
                Draw_nextItem();
                break;
            case LEFT:
                if (_viTri == 1) {
                    changeAnimals();
                } else if (_viTri == 2) {
                    changeLevel();
                } else if (_viTri == 3) {
                    changeSound();
                }
                break;
            case RIGHT:
                if (_viTri == 1) {
                    changeAnimals();
                } else if (_viTri == 2) {
                    changeLevel();
                } else if (_viTri == 3) {
                    changeSound();
                }
                break;
        }

    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }

    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == _cmExit) {
            _midlet.vaoManHinhMenu();
        }
    }
}
